﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using netcoreweb.Areas.Admin.Models;

namespace netcoreweb.Areas.Admin.DataContext
{
    public partial class netcorewebContext : DbContext
    {
        public netcorewebContext()
        {
        }

        public netcorewebContext(DbContextOptions<netcorewebContext> options)
            : base(options)
        {
        }

        public virtual DbSet<User> User { get; set; }
        public virtual DbSet<Article> Articles { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                //Configuration.GetConnectionString("Default");
                optionsBuilder.UseSqlite("Data Source=data/netcoreweb.db");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("ProductVersion", "5.0.0");

            modelBuilder.Entity<User>(entity =>
            {
                entity.ToTable("user");

                entity.Property(e => e.Id)
                    .HasColumnName("id")
                    .ValueGeneratedNever();

                entity.Property(e => e.Password)
                    .HasColumnName("password")
                    .HasColumnType("text(16)");

                entity.Property(e => e.Remark)
                    .HasColumnName("remark")
                    .HasColumnType("text(256)");

                entity.Property(e => e.Roleid).HasColumnName("roleid");

                entity.Property(e => e.Userid).HasColumnName("userid");

                entity.Property(e => e.Username)
                    .HasColumnName("username")
                    .HasColumnType("text(16)");
            });

            modelBuilder.Entity<Article>(entity =>
            {
                entity.ToTable("article");

                entity.Property(e => e.Id)
                    .HasColumnName("id")
                    .ValueGeneratedNever();

                entity.Property(e => e.Title)
                    .HasColumnName("title")
                    .HasColumnType("text(50)");

                entity.Property(e => e.Context)
                    .HasColumnName("context")
                    .HasColumnType("text(10000)");

                entity.Property(e => e.Remark)
                    .HasColumnName("remark")
                    .HasColumnType("text(256)");
            });
        }

        public bool HasUser(string name, string pwd)
        {
            var users = User.ToList();
            var result = users.Where(c => c.Username.Equals(name) && c.Password.Equals(pwd));
            if (result.Count() > 0) return true;
            return false;
        }

        public List<Article> GetArticles()
        {
            return Articles.ToList();
        }

        public Article GetArticle(int id)
        {
            var articles = Articles.ToList().Where(c => c.Id.Equals(id));
            if (articles.Any()) return articles.First();
            return null;
        }

        public int SaveArticle(Article article)
        {
            if (article == null) return -1;
            if (article.Id == 0)
            {
                Articles.Add(article);
            }
            else
            {
                foreach (var a in Articles)
                {
                    if (a.Id == article.Id)
                    {
                        a.Context = article.Context;
                        a.Title = article.Title;
                        a.Remark = article.Remark;
                        break;
                    }
                }
            }
            this.SaveChanges();
            return article.Id;
        }
    }
}
